﻿using EnterpriseWebsiteApi.ActionFilters;
using EnterpriseWebsiteApi.Dto;
using EnterpriseWebsiteBLL;
using EnterpriseWebsiteEntity;
using EnterpriseWebsiteUtil.Helper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EnterpriseWebsiteApi.Controllers.Admin
{
    [ApiController]
    [Route("api/admin/[controller]/[action]")]
    [TypeFilter(typeof(JwtAuthorizeFilter))]
    public class GlobalConfigController : ControllerBase
    {
        private readonly GlobalConfigService _globalConfigService;
        private readonly Mapper _mapper;

        public GlobalConfigController(GlobalConfigService globalConfigService, Mapper mapper)
        {
            _globalConfigService = globalConfigService;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取全局配置信息
        /// </summary>
        [HttpGet]
        public async Task<ActionResult> GetGlobalConfig()
        {
            var globalConfig = await _globalConfigService.GetGlobalConfigAsync();
            if (globalConfig == null)
            {
                globalConfig = new GlobalConfigEntity();
            }
            var globalConfigDto = _mapper.Map<GlobalConfigDto>(globalConfig);
            return Ok(globalConfigDto);
        }

        /// <summary>
        /// 更新全局配置信息
        /// </summary>
        [HttpPost]
        public ActionResult UpdateGlobalConfig(GlobalConfigDto globalConfigDto)
        {
            var entity = _mapper.Map<GlobalConfigEntity>(globalConfigDto);
            _globalConfigService.SaveGlobalConfig(entity);
            return Ok(new { Code = 200,Msg = "更新成功" });
        }
    }
}
